package com.kye.express.business.home;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.dreamlive.upmarqueeview.MarqueeView;
import com.jakewharton.rxbinding2.view.RxView;
import com.kennyc.bottomsheet.BottomSheet;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.base.utils.GsonUtils;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatFragment;
import com.kye.express.business.common_tool.CommonToolActivity;
import com.kye.express.business.customer_intro.CustomerIntroActivity;
import com.kye.express.business.deposit.activity.DepositActivity;
import com.kye.express.business.freight_rates_query.FreightRatesDialogForCallFragment;
import com.kye.express.business.freight_rates_query.FreightRatesDialogFragment;
import com.kye.express.business.freight_rates_query.MounthQueryActivity;
import com.kye.express.business.freight_rates_query.NumberQueryActivity;
import com.kye.express.business.home.productintroduction.ProductIntroductionActivity;
import com.kye.express.business.limit_time_query.LimitTimeQueryActivity;
import com.kye.express.business.login.LoginActivity;
import com.kye.express.business.more.MoreFunctionsActivity;
import com.kye.express.business.notice.notice_center.NoticeCenterActivity;
import com.kye.express.business.online_pay.OnlinePayActivity;
import com.kye.express.business.order_list.OrderListActivity;
import com.kye.express.business.president_online.PresidentOnlineActivity;
import com.kye.express.business.scanner.WaybillScannerActivity;
import com.kye.express.business.waybill_list.WaybillListActivity;
import com.kye.express.business.waybill_search.WaybillSearchActivity;
import com.kye.express.databinding.BottomSheetFreightQueryBinding;
import com.kye.express.databinding.FragmentMainHomeBinding;
import com.kye.express.utils.AppUtil;
import com.kye.express.utils.GlideImageLoader;
import com.kye.express.utils.PhoneUtils;
import com.kye.express.utils.XiaoNengUtils;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.deposit.ResultJudgeStoredQueryBean;
import com.kye.model.bean.freight_rates_query.GetCustomerInfoResultBean;
import com.kye.model.bean.freight_rates_query.PermissionQueryCostResultBean;
import com.kye.model.bean.logistics.LogisticsSendAndReceiveRecordBean;
import com.kye.model.bean.order.OrderStatusType;
import com.kye.model.bean.president_online.PermissionForPresidentOnlineBean;
import com.kye.model.bean.tcl.ResponseTCLGoodsDetailsBean;
import com.kye.model.bean.user.User;
import com.kye.model.bean.waybill_list.WaybillStatusType;
import com.kye.model.constant.Constants;
import com.kye.nb.banner.listener.OnBannerListener;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

//import com.kye.express.business.common_tool_new.CommonToolActivity;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 18/12/2017
 * @updated : 18/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class HomeFragment extends BaseAppCompatFragment implements HomeContract.HomeView, MarqueeView.OnItemClickListener {

    private static final String TAG = HomeFragment.class.getSimpleName();
    protected FragmentMainHomeBinding mDataBinding;
    protected HomeContract.HomePresenter presenter;

    private int mPermissionForPresidentOnline = -1;//-1：还没有请求到服务器的结果，1：有权限，0：没权限
    private String mPermissionForQueryFreightRates = "-1";//-1：还没有请求到服务器的结果，1：有权限，2/5：没权限,3/4：输入验证码
    private String mPermissionForQueryFreightRatesErrorMessage = "";
    private String mPermissionForQueryFreightRatesPhone = "";
    private PermissionQueryCostResultBean mPermissionQueryCostResultBean;
    private Dialog mDialog;

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_home, container, false);
        mDataBinding = DataBindingUtil.bind(view);
        initViews(view, savedInstanceState);
        return view;
    }

    private void initViews(View view, Bundle savedInstanceState) {
        presenter = new HomeContract.HomePresenter(getActivity());
        presenter.bind(this);
        adaptationHeight();

        if (presenter.getUserObservable() != null) {
            presenter.getUserObservable().subscribe(new Consumer<User>() {
                @Override
                public void accept(User user) throws Exception {
                    if (user != null && user.isLogin()) {
                        presenter.checkPermissionForPresidentOnline(false);
                        presenter.checkPermissionForQueryFreightRates(false);
                    }
                }
            });
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initEvents(savedInstanceState);
    }

    private void initEvents(Bundle savedInstanceState) {
        mDataBinding.slContainer.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                updateToolBarStyle();
            }
        });
        mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                presenter.onBannerItemSelected(position);
            }
        });
        mDataBinding.refreshLayout.setOnMultiPurposeListener(new RefreshMultiPurposeListener() {
            @Override
            public void onHeaderPulling(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {
                updateToolbarbarWhenRefreshPulling(header, percent, offset, headerHeight, extendHeight);
            }

            @Override
            public void onHeaderReleasing(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {
                updateToolbarbarWhenRefreshReleasing(header, percent, offset, headerHeight, extendHeight);
            }

            @Override
            public void onHeaderStartAnimator(RefreshHeader header, int headerHeight, int extendHeight) {
                updateToolbarbarWhenRefreshRefreshing(header, headerHeight, extendHeight);
            }

            @Override
            public void onHeaderFinish(RefreshHeader header, boolean success) {
                updateToolbarbarWhenRefreshFinish(header, success);
            }
        });
        mDataBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.initBannerData();
                presenter.initNotification();
                presenter.refreshLogisticsRecordSendReceive();
            }
        });
        /**
         * todo : toolbar
         */
        RxView.clicks(mDataBinding.ivKye)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("kye");
//                        Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
//                        startActivity(loginIntent);
                        presenter.refreshLogisticsRecordSendReceive();
                    }
                });
        RxView.clicks(mDataBinding.llQuery)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        WaybillSearchActivity.startActivity(getActivity());
                    }
                });
        RxView.clicks(mDataBinding.ivScan)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        WaybillScannerActivity.startActivity(getActivity(), WaybillScannerActivity.Type.WAYBILL);
                    }
                });
        /**
         * todo : 取货签收
         */
        RxView.clicks(mDataBinding.llRecords.llRecordTake)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("待取货");
                        checkLogin(getActivity(), new Runnable() {
                            @Override
                            public void run() {
                                startActivity(OrderListActivity.newIntent(getActivity(), OrderStatusType.ORDER_TO_TAKE));
                            }
                        });
                    }
                });
        RxView.clicks(mDataBinding.llRecords.llRecordSign)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("待签收");
                        checkLogin(getActivity(), new Runnable() {
                            @Override
                            public void run() {
                                startActivity(WaybillListActivity.newIntent(getActivity(), WaybillStatusType.WAYBILL_TO_SIGN));
                            }
                        });
                    }
                });
        RxView.clicks(mDataBinding.llRecords.llRecordSigned)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("今日签收");
                        checkLogin(getActivity(), new Runnable() {
                            @Override
                            public void run() {
                                startActivity(WaybillListActivity.newIntent(getActivity(), WaybillStatusType.WAYBILL_SIGNED));
                            }
                        });
                    }
                });
        /**
         * todo : 功能模块
         */
        RxView.clicks(mDataBinding.llFunctions.llProductIntro)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        ProductIntroductionActivity.startActivity(getActivity());
                    }
                });
        RxView.clicks(mDataBinding.llFunctions.llCustomerIntro)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (UserSession.getInstance().getUser() != null && UserSession.getInstance().getUser().isLogin()) {
                            if (UserSession.getInstance().getUser().isEmployee()) {//是内部员工
                                new MaterialDialog.Builder(getActivity())
                                        .content(R.string.customer_intro_no_permission)
                                        .positiveText(R.string.common_know)
                                        .show();
                            } else {
                                CustomerIntroActivity.startActivity(getActivity());
                            }
                        } else {
                            LoginActivity.startActivity(getActivity());
                        }
                    }
                });
        RxView.clicks(mDataBinding.llFunctions.llLogisticsQuery)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (UserSession.getInstance().getUser() != null && UserSession.getInstance().getUser().isLogin()) {
                            showFreightRatesQueryBottomSheet();
                        } else {
                            LoginActivity.startActivity(getActivity());
                        }
                    }
                });
        RxView.clicks(mDataBinding.llFunctions.llTimeQuery)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        LimitTimeQueryActivity.startActivity(getActivity());
                    }
                });
        RxView.clicks(mDataBinding.llFunctions.llOnlinePay)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (UserSession.getInstance().getUser() != null && UserSession.getInstance().getUser().isLogin()) {
                            OnlinePayActivity.startActivity(getActivity());
                        } else {
                            LoginActivity.startActivity(getActivity());
                        }
                    }
                });
        RxView.clicks(mDataBinding.llFunctions.llPrestore)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<>())
                .subscribe((o) -> {
                    Logger.d("预存运费");
                    presenter.checkPermissionForDeposit();
                });
        RxView.clicks(mDataBinding.llFunctions.llTool)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Logger.d("常用工具");
//                        CommonToolActivity.startActivity(getActivity());
                        CommonToolActivity.startActivity(getActivity());
                    }
                });
        RxView.clicks(mDataBinding.llFunctions.llMore)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        //PopularityActivity.startActivity(getActivity());
                        MoreFunctionsActivity.startActivity(getActivity());
                    }
                });
        /**
         * todo : 热线
         */
        RxView.clicks(mDataBinding.llHotline.llPresidentHotLine)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (UserSession.getInstance().getUser() != null && (!TextUtils.isEmpty(UserSession.getInstance().getUser().getToken()))) {
                            if (mPermissionForPresidentOnline == 1) {
                                PresidentOnlineActivity.startActivity(getActivity());
                            } else if (mPermissionForPresidentOnline == -1) {
                                presenter.checkPermissionForPresidentOnline(true);
                            } else if (mPermissionForPresidentOnline == 0) {
                                ToastManager.show(getActivity(), R.string.president_online_no_permission, Toast.LENGTH_SHORT);
                            }
                        } else {
                            LoginActivity.startActivity(getActivity());
                        }
                    }
                });
        RxView.clicks(mDataBinding.llHotline.llOnlineServiceHotLine)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        XiaoNengUtils.startChat(getContext());
                    }
                });
        RxView.clicks(mDataBinding.llHotline.llCustomerHotline)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        final FragmentActivity activity = getActivity();
                        if (activity == null) return;

                        PhoneUtils.call(activity, "4008098098");
                    }
                });
    }

    private void showFreightRatesQueryBottomSheet() {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        BottomSheetFreightQueryBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.bottom_sheet_freight_query, null, false);
        final BottomSheet bottomSheet = new BottomSheet.Builder(getActivity())
                .setView(binding.getRoot()).create();
        bottomSheet.show();
        RxView.clicks(binding.rlMounth)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (UserSession.getInstance().getUser() != null && UserSession.getInstance().getUser().isHasCustomCode()) {
                            if ("1".equals(mPermissionForQueryFreightRates)) {
                                MounthQueryActivity.startActivity(getActivity());
                            } else if ("-1".equals(mPermissionForQueryFreightRates)) {
                                presenter.checkPermissionForQueryFreightRates(true);
                            } else {
                                handleFreightRatesPermission();
                            }
                        } else {
                            if (UserSession.getInstance().getUser() != null && UserSession.getInstance().getUser().isLogin()) {
                                presenter.getCustomerInfo();
                            } else {
                                LoginActivity.startActivity(getActivity());
                            }
                        }

                        bottomSheet.dismiss();
                    }
                });
        RxView.clicks(binding.rlNumber)
                .compose(this.bindToLifecycle())
                .compose(new DebounceObservableTransformer<Object>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        NumberQueryActivity.startActivity(getActivity());
                        bottomSheet.dismiss();
                    }
                });
    }

    /**
     * 处理：当没有权限进入月结运费查询时
     */
    private void handleFreightRatesPermission() {
        if ("2".equals(mPermissionForQueryFreightRates) || "5".equals(mPermissionForQueryFreightRates)) {
            if (!TextUtils.isEmpty(mPermissionForQueryFreightRatesErrorMessage)) {
                new MaterialDialog.Builder(getActivity())
                        .title(getString(R.string.common_reminder))
                        .content(mPermissionForQueryFreightRatesErrorMessage)
                        .positiveText(R.string.common_call)
                        .negativeText(getString(R.string.common_cancel))
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                if (!TextUtils.isEmpty(mPermissionForQueryFreightRatesPhone)) {
                                    PhoneUtils.call(getActivity(), mPermissionForQueryFreightRatesPhone);
                                }
                            }
                        }).show();
            }
        } else if ("3".equals(mPermissionForQueryFreightRates)) {
            if (mPermissionQueryCostResultBean != null) {
                FreightRatesDialogFragment.newInstance(mPermissionQueryCostResultBean).show(getFragmentManager(), "dialog");
            }
        } else if ("4".equals(mPermissionForQueryFreightRates)) {
            if (mPermissionQueryCostResultBean != null) {
                FreightRatesDialogForCallFragment.newInstance(mPermissionQueryCostResultBean).show(getFragmentManager(), "dialog");
            }
        }
    }

    private void updateToolbarbarWhenRefreshPulling(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {
        float np = 1 - percent;
        if (np < 0) {
            np = 0;
        }
        setToolbarViewAllAlph(np);
    }

    private void updateToolbarbarWhenRefreshReleasing(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {
        float np = 1 - percent;
        if (np < 0) {
            np = 0;
        }
        setToolbarViewAllAlph(np);
    }

    private void updateToolbarbarWhenRefreshRefreshing(RefreshHeader header, int headerHeight, int extendHeight) {
        setToolbarViewAllAlph(0);
    }

    private void updateToolbarbarWhenRefreshFinish(RefreshHeader header, boolean success) {

    }

    /**
     * 下拉刷新时更新透明度
     *
     * @param percent
     */
    private void setToolbarViewAllAlph(float percent) {
        float alph = 255 * percent;
        mDataBinding.rlToolbar.setAlpha((int) alph);
        mDataBinding.ivKye.setImageAlpha((int) alph);
        mDataBinding.llQuery.getBackground().setAlpha((int) alph);
        mDataBinding.ivSearchIcon.setImageAlpha((int) alph);
        mDataBinding.tvSearch.setAlpha(percent);
        mDataBinding.ivScan.setImageAlpha((int) alph);
    }

    /**
     * 向上活动时，更新toolbar背景颜色
     */
    private void updateToolBarStyle() {
        int scrollY = mDataBinding.slContainer.getScrollY();
        float percent;
        int toolbarHeight = /*mDataBinding.banner.getHeight() - mDataBinding.vFakeStatusBar.getHeight() -*/ mDataBinding.rlToolbar.getHeight();
        if (scrollY <= toolbarHeight) {
            percent = (scrollY * 1.0f + 0.5f) / toolbarHeight;
            float alpha = 255 * percent;
            mDataBinding.rlToolbar.setBackgroundColor(getResources().getColor(R.color.purple_0));
            mDataBinding.rlToolbar.getBackground().setAlpha((int) alpha);
        } else {
            mDataBinding.rlToolbar.getBackground().setAlpha(255);
        }
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        presenter.initBannerData();
        presenter.initNotification();
        presenter.getUserInfo();
    }

    @Override
    public void initBannerView(ArrayList<String> adImages) {
        Logger.d("images :\n" + GsonUtils.toJsonString(adImages, true));
        mDataBinding.banner.setDelayTime(4000);
        mDataBinding.banner.setImageLoader(new GlideImageLoader());
//        adImages.clear();
//        adImages.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic1xjab4j30ci08cjrv.jpg");
//        adImages.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
        mDataBinding.banner.setImages(adImages);
        mDataBinding.banner.start();
    }

    @Override
    public void onUser(User user) {
        if (user.isLogin()) {
            /**
             * 用户登录后更新订单运单信息
             */
            presenter.getLogisticsSendAndReceiveRecords(user);
            mDataBinding.setIsShowSendReceiveRecords(false);
        } else {
            /**
             * 退出登录后不显示
             */
            mDataBinding.setRecordData(new LogisticsSendAndReceiveRecordBean());
            mDataBinding.setIsShowSendReceiveRecords(false);
        }
    }

    @Override
    public void initNoticeMarquee(List<View> views) {
        mDataBinding.marqueeView.setViews(views);
        mDataBinding.marqueeView.setOnItemClickListener(this);
        if (views.size() <= 1) {
            mDataBinding.marqueeView.stopFlipping();
        }
    }

    @Override
    public void onDepositLoading() {

    }

    @Override
    public void onDepositComplete() {

    }

    @Override
    public void onDepositSuccess(OpenApiResult<ResultJudgeStoredQueryBean> bean) {
        if (bean != null) {
            if (bean.getCode() == Constants.ResponseCode_SUCCESS) {
                DepositActivity.startActivity();
            } else {
                ToastManager.show(getActivity(), "月结客户无法使用此功能");
            }
        } else {
            Logger.d(TAG + " - onDepositSuccess - bean is null");
        }
    }

    @Override
    public void onDepositError(OpenApiResult<ResultJudgeStoredQueryBean> result, int code, String msg) {
        ToastManager.show(getActivity(), msg);
        Logger.d(TAG + " - onDepositError - result:" + result + ",code:" + code + ",msg:" + msg);
    }

    @Override
    public void onDepositOtherError(Throwable e) {
        ToastManager.show(getActivity(), getString(R.string.common_net_error));
        Logger.d(TAG + " - onDepositOtherError : " + e.getMessage());
        e.printStackTrace();
    }

    @Override
    public void updateLogisticsSendAndReceiveRecords(LogisticsSendAndReceiveRecordBean logisticsSendAndReceiveRecordBeans) {
        mDataBinding.setRecordData(logisticsSendAndReceiveRecordBeans);
        mDataBinding.setIsShowSendReceiveRecords(true);
    }

    @Override
    public void getLogisticsSendAndReceiveRecordsFailed(OpenApiResult<LogisticsSendAndReceiveRecordBean> result) {
        mDataBinding.setIsShowSendReceiveRecords(false);
    }

    @Override
    public void getLogisticsSendAndReceiveRecordsFailed(Throwable throwable) {
        mDataBinding.setIsShowSendReceiveRecords(false);
    }

    @Override
    public void onItemClick(int position, View view) {
        startActivity(NoticeCenterActivity.newIntent(getActivity()));
    }

    @Override
    public void onStart() {
        super.onStart();
        mDataBinding.banner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        mDataBinding.banner.stopAutoPlay();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.unbind();
    }

    @Override
    public FragmentMainHomeBinding getDataBinding() {
        return mDataBinding;
    }


    @Override
    public void onPresidentOnlinePermission(PermissionForPresidentOnlineBean permissionForPresidentOnline, boolean startPresidentOnlinePage) {
        mPermissionForPresidentOnline = permissionForPresidentOnline.isIsCan() ? 1 : 0;
        if (mPermissionForPresidentOnline == 1 && startPresidentOnlinePage) {
            PresidentOnlineActivity.startActivity(getActivity());
        }
    }

    public void adaptationHeight() {
        // 如果系统版本是4.4版本就要适配高度（19-20）
        if (AppUtil.getSDKVersion() == 19 || AppUtil.getSDKVersion() == 20) {
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0, AppUtil.getStatusBarHeight(), 0, 0);
            mDataBinding.framelayoutHome.setLayoutParams(lp);
        }
    }

    @Override
    public void onPermissionForQueryFreightRates(OpenApiResult<PermissionQueryCostResultBean> resultBean, boolean startFreightRatesPage) {
        if (resultBean != null) {
            if (resultBean.getData() != null) {
                if ("1".equals(resultBean.getData().getStatus())) {
                    mPermissionForQueryFreightRates = "1";
                    if (startFreightRatesPage) {
                        MounthQueryActivity.startActivity(getActivity());
                    }
                } else {
                    mPermissionForQueryFreightRates = resultBean.getData().getStatus();
                    mPermissionForQueryFreightRatesPhone = resultBean.getData().getPhone();
                    mPermissionQueryCostResultBean = resultBean.getData();
                    if (startFreightRatesPage) {
                        handleFreightRatesPermission();
                    }
                }
            }
            mPermissionForQueryFreightRatesErrorMessage = resultBean.getMsg();

        }
    }

    @Override
    public void onGetCustomerInfoLoaded(GetCustomerInfoResultBean resultBean) {
        //TODO 待开发
        ToastManager.show(getActivity(), "跳转到绑定客户编码页面", Toast.LENGTH_SHORT);
    }

    @Override
    public void onGetCustomerInfoComplete() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onGetCustomerInfoError(String msg) {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onGetCustomerInfoLoading() {
        if (mDialog == null) {
            mDialog = new KyeProgressDialog.Builder(getActivity()).build();
        }
        mDialog.show();
    }

    @Override
    public Context getCtx() {
        return getActivity();
    }
}
